//
//  RAPVisitorView.swift
//  COPYCAT-Weibo
//
//  Created by 闫萌 on 12/28/15.
//  Copyright © 2015 RendezvousAuParadis. All rights reserved.
//

import UIKit

// MARK: - 注意，SW的协议必须继承自NSObjectProtocol
protocol RAPVisitorViewDelegate: NSObjectProtocol {
    func loginButtonClicked()
    func signupButtonClicked()
}

class RAPVisitorView: UIView {
    
    // 代理属性
    weak var delegate: RAPVisitorViewDelegate?
    
    var hasCircle = false
    
    // MARK: - 懒加载控件
    // 中间图片
    private lazy var iconView: UIImageView = UIImageView()
    // 圆圈图片
    private lazy var circleView: UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return imgView
        }()
    
    // 遮罩图片
    private lazy var circleMaskView: UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return imgView
        }()
    // 标签
    private lazy var messageLabel: UILabel = {
        let label = UILabel(text: nil, fontSize: 13)
        label.textAlignment = .Center
        label.numberOfLines = 0
        return label
        }()
    // 登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton(title: "登录", fontSize: 14, imageName: nil, backgroundImageName: "common_button_white_disable", target: self, action: "buttonPressed:")
        return button
        }()
    
    // 注册按钮
    private lazy var signupButton: UIButton = {
        let button = UIButton(title: "注册", fontSize: 14, imageName: nil, backgroundImageName: "common_button_white_disable", target: self, action: "buttonPressed:", textColor: UIColor.darkGrayColor())
        return button
        }()
    
    
    // MARK: - 自定义方法
    
    func buttonPressed(sender: UIButton) {
        
        if delegate != nil {
            sender === loginButton ? delegate?.loginButtonClicked() : delegate?.signupButtonClicked()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 237 / 255, alpha: 1.0)
    }

    /**
    从xib创建view，会调用这个指定初始化方法，
    如果误用这个方法，报错以提示
    */
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /**
    设置子控件，这个方法必须在初始化之后立即调用
    */
    func setupVisitorView(iconName: String, messageText: String, hasCircle: Bool = false) {
        
        
        
        iconView.image = UIImage(named: iconName)
        iconView.sizeToFit()
        messageLabel.text = messageText
        self.hasCircle = hasCircle
        // 如果有圆圈
        if hasCircle {
            // 将圆圈和遮罩加入视图
            addSubview(circleView)
            addSubview(circleMaskView)
            
            // 让circle开始转动
            let anim = CABasicAnimation(keyPath: "transform.rotation")
            anim.duration = 15
            anim.toValue = 2 * M_PI
            anim.repeatCount = MAXFLOAT
            anim.removedOnCompletion = false
            circleView.layer.addAnimation(anim, forKey: nil)
        }
        
        // 加入其他控件
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(signupButton)
        
        installConstraints()
        
        // 设置导航栏
        

    }

    private func installConstraints() {
        // 在这个方法中为子控件添加约束
        // TODO: 使用snapKit修改以下约束
        // 取消autoresizing
        iconView.translatesAutoresizingMaskIntoConstraints = false
        circleView.translatesAutoresizingMaskIntoConstraints = false
        circleMaskView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        signupButton.translatesAutoresizingMaskIntoConstraints = false
        
        // 开始设置约束，擦
        // iconView
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
        if hasCircle {
            
            // circleView
            addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
            addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
            
            // circleMaskView
            addConstraint(NSLayoutConstraint(item: circleMaskView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
            addConstraint(NSLayoutConstraint(item: circleMaskView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
        }
        
        
        // messageLabel
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: kScreenBounds.width * 0.7))

        
        // loginButton
        // y
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        // w
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: kScreenBounds.width * 0.3))
        // x
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Leading, relatedBy: .Equal, toItem: messageLabel, attribute: .CenterX, multiplier: 1, constant: -kScreenBounds.width * 0.35))
        
        // signupButton
        // y
        addConstraint(NSLayoutConstraint(item: signupButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: signupButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: kScreenBounds.width * 0.3))
        addConstraint(NSLayoutConstraint(item: signupButton, attribute: .Leading, relatedBy: .Equal, toItem: messageLabel, attribute: .CenterX, multiplier: 1, constant: kScreenBounds.width * 0.05))
        
    }

    

    


    
}
